﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 系统日志
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.yinmaisoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.20
    /// </summary>
    internal class LogService : DataFactory, LogIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="category">日志分类</param>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<LogEntity>> GetList(int category, PageInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<LogEntity>().And(m => m.Category == category);
                var queryParam = input.queryJson.ToObject();
                //日期范围（近7天、近1月、近3月、自定义）
                if (!queryParam["startTime"].IsEmpty() && !queryParam["endTime"].IsEmpty())
                {
                    var startTime = CommonHelper.GetDateTime(queryParam["startTime"].ToString());
                    var endTime = CommonHelper.GetDateTime(queryParam["endTime"].ToString());
                    startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, 0, 0, 0, 0);
                    endTime = new DateTime(endTime.Year, endTime.Month, endTime.Day, 23, 59, 59, 999);
                    queryWhere = queryWhere.And(t => t.CreatorTime >= startTime && t.CreatorTime <= endTime);
                }
                //关键字（用户、IP地址、功能名称）
                if (!string.IsNullOrEmpty(input.keyword))
                {
                    queryWhere = queryWhere.And(m => m.UserName.Contains(input.keyword) || m.IPAddress.Contains(input.keyword) || m.ModuleName.Contains(input.keyword));
                }
                //用户Id
                if (!queryParam["userId"].IsEmpty() && !queryParam["userAccount"].IsEmpty())
                {
                    var userId = queryParam["userId"].ToString();
                    var userAccount = queryParam["userAccount"].ToString();
                    queryWhere = queryWhere.And(t => t.UserId == userId || t.UserId == userAccount);
                }
                return await db.GetIQueryable<LogEntity>().Where(queryWhere).OrderByDescending(t => t.CreatorTime).GetPageResultAsync(input);

            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<LogEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<LogEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids">id集合</param>
        public async Task Delete(object[] ids)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                foreach (string item in ids)
                {
                    db.Delete<LogEntity>(m => m.Id == item);
                }
                await db.CommitTransactionAsync();
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(LogEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
    }
}
